R中的数据类型与数据结构

R
Author

Tony Duan

Published

January 26, 2023

1. 查看版本

Code
#update R
#R.version


#Update Rstudio 
#rstudioapi::versionInfo()


#check R package location
#.libPaths()


#Update R package

# list all packages where an update is available
#old.packages()

# update all available packages
#update.packages()

2. 数据类型 datatype

##character

当检查变量懂的数据类型或者数据结构,建议使用class()而不是typeof()

Code
x <- "dataset"
class(x)
[1] "character"

##numeric

Code
x <- 123
class(x)
[1] "numeric"

##complex

Code
x <- 3 + 2i
class(x)
[1] "complex"
Code
x <- TRUE
class(x)
[1] "logical"

数据结构 data structures

Vector

One number is a vector too.

Code
y <- 1
y
[1] 1
Code
class(y)
[1] "numeric"
Code
y <- c(1,2,3)
y
[1] 1 2 3
Code
class(y)
[1] "numeric"

create vector

Code
seq(from = 2, to = 14, by = 2) 
[1]  2  4  6  8 10 12 14
Code
rep(x = 1.5, times = 4)  
[1] 1.5 1.5 1.5 1.5

append vector

Code
x=c(1,2,3)
y=c(4,5,6)
z=c(x,y)
z
[1] 1 2 3 4 5 6

calculate vector

Code
x=c(1,2,3)
Code
sum(x)
[1] 6
Code
mean(x)
[1] 2
Code
x+10
[1] 11 12 13

Matrix

Code
x <- matrix(201:216, nrow = 4)
y <- matrix(1:16, nrow = 4)
class(y)
[1] "matrix" "array" 
Code
x
     [,1] [,2] [,3] [,4]
[1,]  201  205  209  213
[2,]  202  206  210  214
[3,]  203  207  211  215
[4,]  204  208  212  216
Code
y
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16

check size,it 4 by 4 matrix

Code
dim(x)
[1] 4 4

check row

Code
nrow(x)
[1] 4

check column

Code
ncol(x)
[1] 4

matrix calculation

Transpose

Code
t(y)
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16

row append two matrix

Code
rbind(x,y)
     [,1] [,2] [,3] [,4]
[1,]  201  205  209  213
[2,]  202  206  210  214
[3,]  203  207  211  215
[4,]  204  208  212  216
[5,]    1    5    9   13
[6,]    2    6   10   14
[7,]    3    7   11   15
[8,]    4    8   12   16

column append two matrix

Code
cbind(x,y)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]  201  205  209  213    1    5    9   13
[2,]  202  206  210  214    2    6   10   14
[3,]  203  207  211  215    3    7   11   15
[4,]  204  208  212  216    4    8   12   16
Code
x+y
     [,1] [,2] [,3] [,4]
[1,]  202  210  218  226
[2,]  204  212  220  228
[3,]  206  214  222  230
[4,]  208  216  224  232

select matrix elements

selects the element at the first row

Code
y[1,] 
[1]  1  5  9 13

select the 6th element at the matrix

Code
y[6] 
[1] 6

selects the element at the 1,2 row

Code
y[1:2,] 
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14

selects the element at the first column

Code
y[,1] 
[1] 1 2 3 4

selects the element at the first row and second column.

Code
y[1,2] 
[1] 5

List

Code
y <- list(c("black", "yellow", "orange"),
               c(TRUE, TRUE, FALSE, TRUE, FALSE, FALSE),
               matrix(1:6, nrow = 3))
class(y)
[1] "list"

Dataframe

Code
height <- c(180, 155, 160, 167, 181)
weight <- c(65, 50, 52, 58, 70)
names <- c("Joanna", "Charlotte", "Helen", "Karen", "Amy")

y <- data.frame(height = height, weight = weight, names = names)
x1=y[2,]

x2=y[,1]
class(y)
[1] "data.frame"
Code
class(y$height)
[1] "numeric"
Code
class(y$names)
[1] "character"
Code
y
  height weight     names
1    180     65    Joanna
2    155     50 Charlotte
3    160     52     Helen
4    167     58     Karen
5    181     70       Amy

check size,it 5 row by 3 column data frame

Code
dim(y)
[1] 5 3

check row

Code
nrow(y)
[1] 5

check column

Code
ncol(y)
[1] 3

dataframe calculation

Transpose

Code
z=t(y)
z
       [,1]     [,2]        [,3]    [,4]    [,5] 
height "180"    "155"       "160"   "167"   "181"
weight "65"     "50"        "52"    "58"    "70" 
names  "Joanna" "Charlotte" "Helen" "Karen" "Amy"

row append two dataframe

Code
rbind(y,x1)
   height weight     names
1     180     65    Joanna
2     155     50 Charlotte
3     160     52     Helen
4     167     58     Karen
5     181     70       Amy
21    155     50 Charlotte

column append two dataframe

Code
cbind(y,x2)
  height weight     names  x2
1    180     65    Joanna 180
2    155     50 Charlotte 155
3    160     52     Helen 160
4    167     58     Karen 167
5    181     70       Amy 181

select dataframe elements

selects the element at the first row

Code
y[1,] 
  height weight  names
1    180     65 Joanna

selects the element at the 1,2 row

Code
y[1:2,] 
  height weight     names
1    180     65    Joanna
2    155     50 Charlotte

selects the element at the first and second column

Code
y[,1:2] 
  height weight
1    180     65
2    155     50
3    160     52
4    167     58
5    181     70

selects the element at the first row and second column.

Code
y[1,2] 
[1] 65

Reference

http://adv-r.had.co.nz/Data-structures.html#vectors